---------------------
-- function get_mason_adapter
---------------------
function _G.get_mason_adapter(adapter)
  local adp = ""
  if WINDOWS() then
    adp = vim.fn.expand("$HOME\\.leovim.d\\mason\\bin\\") .. adapter .. ".cmd"
  elseif UNIX() then
    adp = vim.fn.expand("$HOME/.leovim.d/mason/bin/")  .. adapter
  else
    return nil
  end
  if vim.fn.filereadable(adp) > 0 then
    return adp
  else
    return nil
  end
end
---------------------
-- setting each dap
---------------------
local dap = require('dap')
-- python
dap.adapters.python = function(cb, config)
  if config.request == 'attach' then
    local port = (config.connect or config).port
    local host = (config.connect or config).host or '127.0.0.1'
    cb({
      type = 'server',
      port = assert(port, '`connect.port` is required for a python `attach` configuration'),
      host = host,
      options = {
        source_filetype = 'python',
      },
    })
  else
    cb({
      type = 'executable',
      command = 'python',
      args = { '-m', 'debugpy.adapter' },
      options = {
        source_filetype = 'python',
      },
    })
  end
end
dap.configurations.python = {
  {
    type = 'python',
    request = 'launch',
    name = "Launch file",
    program = "${file}",
    pythonPath = function()
      -- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
      -- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
      -- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable.
      local cwd = vim.fn.getcwd()
      if vim.fn.executable(cwd .. '/venv/bin/python') == 1 then
        return cwd .. '/venv/bin/python'
      elseif vim.fn.executable(cwd .. '/.venv/bin/python') == 1 then
        return cwd .. '/.venv/bin/python'
      else
        return 'python'
      end
    end
  }
}
-- debugger for cpp rust
if vim.g.dap_cpprust ~= 'cppdbg' then
  dap.adapters.codelldb = {
    type = 'server',
    port = "${port}",
    executable = {
      -- CHANGE THIS to your path!
      command = get_mason_adapter('codelldb'),
      args = {"--port", "${port}"},
      -- On windows you may have to uncomment this:
      -- detached = false,
    }
  }
  dap.configurations.cpp = {
    {
      name = "Launch file",
      type = "codelldb",
      request = "launch",
      program = function()
        return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
      end,
      cwd = "${workspaceFolder}",
      stopOnEntry = false,
    }
  }
else
  dap.adapters.cppdbg = {
    id = 'cppdbg',
    type = 'executable',
    command = get_mason_adapter('OpenDebugAD7'),
    -- options = {
    --   detached = false
    -- }
  }
  dap.configurations.cpp = {
    {
      name = "Launch file",
      type = "cppdbg",
      request = "launch",
      program = function()
        return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
      end,
      cwd = "${workspaceFolder}",
      stopOnEntry = true,
    }
  }
end
dap.configurations.c    = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
-- node
dap.adapters.node       = {
  type    = 'executable';
  command = 'node';
  args    = { get_mason_adapter('nodeDebug.js') };
}

